package nl.saxion.act.playground;

import nl.saxion.act.main_menu.HighscoresActivity;
import nl.saxion.act.main_menu.MenuActivity;
import nl.saxion.act.playground.R.menu;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.webkit.WebView.FindListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

public class MyGameActivity extends Activity implements OnTouchListener {
	private static GameView mGameView;
	private static Button bluePlayerScore;
	private static Button greenPlayerScore;
	private static Button currentPlayer;
	private static TextView playerWon;

	boolean soundDisabled;
	private boolean highscorePlayer1 = true;
	private boolean highscorePlayer2 = false;

	static EditText editText1;
	static EditText editText2;

	static final int MIN_DISTANCE = 100;
	private float downX, downY, upX, upY;




	/** Called when the activity is first created. 
	 * Checks if the sound is on or off
	 */
	@Override

	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.sound, menu);

		MenuItem item = menu.findItem(R.id.item1);
		//Handle item selection


		if(soundDisabled == false) {
			AudioManager audioManager = (AudioManager) getSystemService(MyGameActivity.AUDIO_SERVICE); audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 10, 0);
			item.setTitle("Geluid Aan");

		} else {
			AudioManager audioManager = (AudioManager) getSystemService(MyGameActivity.AUDIO_SERVICE); audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0);
			item.setTitle("Geluid Uit");
		}


		if(soundDisabled == true) {
			soundDisabled = false;

		} else {
			soundDisabled = true;
		}
		return super.onPrepareOptionsMenu(menu);
	}



	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		mGameView = (GameView) findViewById(R.id.game);
		mGameView.initNewGame();
		mGameView.setOnTouchListener(this);
		bluePlayerScore = (Button) findViewById(R.id.scoreLabelBluePlayer);
		greenPlayerScore = (Button) findViewById(R.id.scoreLabelGreenPlayer);
		currentPlayer = (Button) findViewById(R.id.currentPlayerLabel);
		playerWon = (TextView) findViewById(R.id.textFieldPlayerWon);

		linksButton();
		rechtsButton();
		kickBallButton();
	}



	/*
	 * Updates the PUNTEN view, gets the amount of points
	 */

	public static void updateScoreLabels(Player player1, Player player2) {
		bluePlayerScore.setText("" + GameView.getScore(player1) + " PUNTEN");
		greenPlayerScore.setText("" + GameView.getScore(player2) + " PUNTEN");
	}

	/*
	 * Sets the text in the middle of the game whose turn it is
	 */
	public static void updatePlayerTurnLabel(Player player1, Player player2) {
		currentPlayer.setText("" + GameView.getCurrentPlayer(player1) + GameView.getCurrentPlayer(player2) + " IS AAN ZET");	
	}
	/*
	 * Makes sure the player has his name for the above code
	 */
	public static void setTurnLabel(String text) {
		currentPlayer.setText(text);
	}


	/*
	 * When the game is over, a menu pops up where you can type in the name of player 1 & 2 for the highscore
	 */

	public static void gameOverPopUp(Context mcontext) {
		LayoutInflater layoutInflater = (LayoutInflater)mcontext.getSystemService(MyGameActivity.LAYOUT_INFLATER_SERVICE); 
		View popupView = layoutInflater.inflate(R.layout.gameover, null); 
		PopupWindow popupWindow = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		popupWindow.setFocusable(true);
		Log.d("Debug", " hoi 1");
		popupWindow.showAtLocation(popupView, 0, 87, 70);
		Log.d("Debug", " hoi 2");
		editText1 = (EditText) popupView.findViewById(R.id.editTextHighscores1);
		editText2 = (EditText) popupView.findViewById(R.id.editTextHighscores2);

		CheckBox checkBox1 = (CheckBox) popupView.findViewById(R.id.checkBox1);
		checkBox1.setChecked(true);

		TextView textField = (TextView) popupView.findViewById(R.id.textFieldPlayerWon);
		int[] scores = mGameView.getScores();
		int score = 0;
		String player = "";
		if(scores[0] > scores[1]) {
			score = scores[0];
			player = "Blue";
		} else if(scores[0] < scores[1]) {
			score = scores[1];
			player = "Green";
		}
		textField.setText(player + " player has won with " + Integer.toString(score) + " points.");
	}
	/*
	 * Restarts the game
	 */
	public void gameRestart(View v){
		Intent intent = new Intent(MyGameActivity.this, MyGameActivity.class);
		startActivity(intent);
	}
	/*
	 * Opens the main menu
	 */

	public void openMainMenu(View v){
		Intent intent = new Intent(MyGameActivity.this, MenuActivity.class);
		startActivity(intent);
	}
	/*
	 * can check and uncheck the boxes to save the highscore
	 */
	public void onCheckBoxClicked(View view){
		boolean checked = ((CheckBox) view).isChecked();

		switch(view.getId()) {
		case R.id.checkBox1:
			if (checked)
				highscorePlayer1 = true;
			else
				highscorePlayer1 = false;
			break;
		case R.id.checkBox2:
			if (checked)
				highscorePlayer2 = true;
			else
				highscorePlayer2 = false;
			break;
		}
	}

	/*
	 * Gets the current score and makes it able to input into a list
	 */
	public void saveHighscore(View view){
		int[] scores = mGameView.getScores();
		int score1 = scores[0];
		int score2 = scores[1];

		String name1 = editText1.getText().toString();
		String name2 = editText2.getText().toString();
		if((highscorePlayer1 && name1.length() > 0) || (highscorePlayer2 && name2.length() > 0)) {
			Intent intent = new Intent(MyGameActivity.this, HighscoresActivity.class);
			if(highscorePlayer1 && name1.length() > 0) {
				intent.putExtra("score1", score1);
				intent.putExtra("name1", editText1.getText().toString());
			}
			if(highscorePlayer2 && name2.length() > 0) {
				intent.putExtra("score2", score2);
				intent.putExtra("name2", editText2.getText().toString());
			}
			startActivity(intent);
		}
	}
	/*
	 * Implements the button which allows the player to move to the left
	 */
	private void linksButton() {
		final View buttonLinks = (View) findViewById(R.id.scoreLabelBluePlayer);
		buttonLinks.setSoundEffectsEnabled(false);
		buttonLinks.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				mGameView.movePlayerLeft();
			}
		});
	}
	/*
	 * Implements the button which allows the player to move to the right
	 */

	private void rechtsButton() { 
		final View buttonRechts = (View) findViewById(R.id.scoreLabelGreenPlayer);
		buttonRechts.setSoundEffectsEnabled(false);
		buttonRechts.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				mGameView.movePlayerRight();
			}
		});
	}


	/*
	 * Implements the button which allows the player to kick the ball
	 */
	private void kickBallButton() {

		final View buttonSchop = (View) findViewById(R.id.currentPlayerLabel);
		final MediaPlayer mp = MediaPlayer.create(this, R.raw.moveballmp3);
		buttonSchop.setSoundEffectsEnabled(false);
		buttonSchop.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (mGameView.unfinishedBalls() == false) {
					mGameView.kickBall();
					mp.start();
				}
			}
		});

	}

	public void onRightToLeftSwipe(){
		mGameView.swapBallRow();
	}

	public void onLeftToRightSwipe(){
		mGameView.swapBallRow();
	}

	/*
	 * Horizontal swipes make the ball row switch from player
	 */
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		switch(event.getAction()){
		case MotionEvent.ACTION_DOWN: {
			downX = event.getX();
			downY = event.getY();
			return true;
		}
		case MotionEvent.ACTION_UP: {
			upX = event.getX();
			upY = event.getY();

			float deltaX = downX - upX;

			// swipe horizontal?
			if(Math.abs(deltaX) > MIN_DISTANCE){
				// left or right
				if(deltaX < 0) { this.onLeftToRightSwipe(); return true; }
				if(deltaX > 0) { this.onRightToLeftSwipe(); return true; }
			}
			else {

				return false; 
			}
			return true;
		}
		}
		return false;
	}


}


